#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tornado
import tornado.web
from utils import log
import json
from config import etc
from controls import data_sight_base_mysql
from controls import data_sight_desc_mysql
from controls import data_user_brief_mysql
from controls import data_sight_comment_mongo
from controls import data_sight_desc_title_mysql
from utils.session_base import ApiGuest
from utils.session_mc import BaseHandler
from socket_server import comment_upd_que


class DictHandler( BaseHandler ) :
    
    @ApiGuest
    def post( self ) :
        cityid = self.ss_params['cityid']
        log.i( 'cityid=%s' % (cityid) )
        if not cityid :
            log.w( 'cityid fetching error' )
            res = { 'idx':self.ss_idx, 'ret':1, 'msg': etc.err_500, 'res':{} }
            self.write( json.dumps(res) )
            self.finish()
            return
        sight_base = data_sight_base_mysql.get_sight_base_all( cityid )
        sight_list = list(sight_base.keys())
        log.i(sight_list)
        sight_desc = data_sight_desc_mysql.get_sight_desc_all( cityid, sight_list )
        log.i(sight_desc)
        ''' delete sight's img info 2015/05/04
        sight_imgs_raw = data_sight_img_mysql.get_sight_img_all( cityid )
        imgid_dict = data_img_base_mysql.get_img_url_by_imgid_list( sight_imgs_raw['imgid_list'] )
        sight_imgs = sight_imgs_raw['res']
        for sightid in sight_imgs_raw[ 'sightid_list' ] :
            imgs = sight_imgs[sightid]['imgs']
            for img in imgs :
                img[ 'url' ] = imgid_dict[ int(img['url']) ]
        '''
        res_res = {}
        for key in sight_base.keys() :
            tmp = sight_base[key]
            tmp['desc'] = sight_desc[key]
            ''' delete sight's img info 2015/05/04
            if sight_imgs.has_key(key):
                tmp.update( sight_imgs[key] )
            '''
            res_res[key] = tmp
        res = { 'idx':self.ss_idx, 'ret':0, 'msg':'ok', 'res':res_res }
        log.i ( res )
        self.write( json.dumps(res) )
        self.finish()
        return

class SightDetailHandler( BaseHandler ) :
    
    @ApiGuest
    def post( self ) :
        cityid = self.ss_params['cityid']
        sightid = self.ss_params['sightid']
        log.i( 'cityid=%s, sightid=%s' % (cityid, sightid) )
        if not ( cityid and sightid ):
            log.w( 'cityid and sightid fetching error' )
            res = { 'idx':self.ss_idx, 'ret':1, 'msg': etc.err_500, 'res':{} }
            self.write( json.dumps(res) )
            self.finish()
            return
        sight_base = data_sight_base_mysql.get_sight_base_by_sightid( int(cityid), int(sightid) )
        if not sight_base:
            log.w( 'sight_base fetching error' )
            res = { 'idx':self.ss_idx, 'ret':1, 'msg': etc.err_500, 'res':{} }
            self.write( json.dumps(res) )
            self.finish()
            return

        sight_desc_dict = data_sight_desc_mysql.get_sight_desc_by_sightid( int(cityid), int(sightid) )
        sight_base['desc'] = sight_desc_dict[ int(sightid) ] if sight_desc_dict else []
 
        ''' delete sight's img info 2015/05/04
        sight_imgs_raw = data_sight_img_mysql.get_sight_img_by_sightid( int(cityid), int(sightid) )
        imgid_dict = data_img_base_mysql.get_img_url_by_imgid_list( sight_imgs_raw['imgid_list'] )

        sight_imgs = sight_imgs_raw['res']
        for img in sight_imgs :
            img[ 'url' ] = imgid_dict[ int(img['url']) ]
        
        sight_base['imgs'] = sight_imgs
        '''
        
        res = { 'idx':0, 'ret':0, 'msg':'ok', 'res':sight_base }
        log.i ( res )
        self.write( json.dumps(res) )
        self.finish()
        return

class SightDescTitleHandler( BaseHandler ) :
    
    @ApiGuest
    def post( self ) :
        title_list = data_sight_desc_title_mysql.get_sight_desc_title_all()
        res = { 'idx':self.ss_idx, 'ret':0, 'msg':'ok', 'res':title_list }
        self.write( json.dumps(res) )
        self.finish()
        return

class GetCommentHandler( BaseHandler ) :

    @ApiGuest
    def post( self ) :
        #cityid = self.get_argument( 'cityid', default=99 ) #for get method test
        #sightid = self.get_argument( 'sightid', default=1 ) #for get method test
        cityid = self.ss_params['cityid']
        sightid = self.ss_params['sightid']
        log.i( 'cityid=%s, sightid=%s' % (cityid, sightid) )
        if not cityid or not sightid:
            log.w( 'cityid fetching error' )
            res = { 'idx':self.ss_idx, 'ret':1, 'msg': etc.err_500, 'res':{} }
            self.write( json.dumps(res) )
            self.finish()
            return

        comment = data_sight_comment_mongo.get_sight_comment(int(cityid), int(sightid))
        log.i ( comment )
        res = { 'idx':self.ss_idx, 'ret':0, 'msg': 'ok', 'res':comment }
        
        self.write( json.dumps(res) )
        self.finish()
        return

class AddCommentHandler( BaseHandler ) :

    @ApiGuest
    def post( self ) :
        cityid = self.ss_params['cityid']
        sightid = self.ss_params['sightid']
        userid = self.ss_params['userid']
        comment = self.ss_params['content']
        #cityid = self.get_argument( 'cityid', default=99)
        #sightid = self.get_argument( 'sightid', default=1)
        #userid = self.get_argument( 'userid', default=111001)
        #comment = self.get_argument( 'content', default='天地有大，美而不言')
        log.i( 'cityid=%s, sightid=%s, userid=%s, comment=%s' % (cityid, sightid, userid, comment) )
        if not cityid or not sightid or not userid or not comment:
            log.w( 'comment content fetching error' )
            res = { 'idx':self.ss_idx, 'ret':1, 'msg': etc.err_500, 'res':{} }
            self.write( json.dumps(res) )
            self.finish()
            return

        res = {
            'idx':self.ss_idx,  #self.ss_idx,
            'ret':0,
            'msg':'ok',
            'res':{}
        }
        username = ''
        user_brief = data_user_brief_mysql.get_user_brief_by_userid(userid)
        if user_brief:
            username = user_brief['name']
        result = data_sight_comment_mongo.add_sight_comment(int(cityid), int(sightid), int(userid), comment, username)
        log.i ( result )
        if not result :
            res['ret'] = 2
            res['msg'] = 'ng'
        else :
            #comment_upd_que.put({'cityid':99, 'sightid':1})
            comment_upd_que.put({'cityid':int(cityid), 'sightid':int(sightid)})

        comment = data_sight_comment_mongo.get_sight_comment(int(cityid), int(sightid))
        log.i ( comment )
        res['res'] = comment

        self.write( json.dumps(res) )
        self.finish()
        return

class ApiNotFindHandler( BaseHandler ) :
    
    def get( self ):
        raise tornado.web.HTTPError( 503 )
    @tornado.web.asynchronous
    def post( self ) :
        log.i( self )
        res = { 'idx':0, 'ret':9, 'msg':'无此功能', 'res':{} }
        self.write( json.dumps(res) )
        self.finish()

